/*
 * Copyright (c) 2018 Hitevision
 */

package tw.com.hitevision.whiteboard.android.util;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.Log;
import android.view.Surface;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * This helper class provides access to {@code android.view.SurfaceControl} for screenshot features.
 */
public class SurfaceControlUtils {
    public static Bitmap takeScreenshot(int width, int height) {
        try {
            Class<?> c = Class.forName("android.view.SurfaceControl");
            Method screenshot = c.getMethod("screenshot", int.class, int.class);
            return (Bitmap) screenshot.invoke(null, width, height);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static Bitmap startScreenShot(int width, int height) {
        Bitmap sBitmap = null;
        try {
            @SuppressLint("PrivateApi") Class<?> mClassType = Class.forName("android.view.SurfaceControl");
            Method nativeScreenshotMethod;
            nativeScreenshotMethod = mClassType.getDeclaredMethod("screenshot", Rect.class, int.class, int.class, int.class);
            nativeScreenshotMethod.setAccessible(true);
            sBitmap = (Bitmap) nativeScreenshotMethod.invoke(mClassType, new Rect(), width, height, Surface.ROTATION_0);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return sBitmap;
        }
    }
}
